$(function (){
    $("#mobileError").hide();
    $("#passwordError").hide();
    dealInputPlaceholder();
})

// 校验手机号码
function checkMobile(){
    var mobile = $("#mobile").val();
    if(mobile == null || mobile == ""){
        $("#mobileError").text("请输入手机号或邮箱号码!");
        $("#mobileError").show();
        return false;
    }
    var mobileReg = /^1\d{10}$/;
    var emailReg = /^\w+[@]\w{2,5}([.]\w{2,3}){1,3}$/i;
    if(!mobileReg.test(mobile) && !emailReg.test(mobile)){
        $("#mobileError").text("手机号或邮箱号码输入格式有误!");
        $("#mobileError").show();
        return false;
    }
    $("#mobileError").hide();
    return true;
}

// 校验密码
function checkPassword(){
    var password = $("#password").val();
    if(password == null || password == ""){
        $("#passwordError").text("请输入密码!");
        $("#passwordError").show();
        return false;
    }
    $("#passwordError").hide();
    return true;
}

// 发送验证码
function sendCheckCode(){
    if(!checkMobile()){
        return false;
    }
    if(!checkPassword()){
        return false;
    }

    var timer = setInterval(beginTime,1000);
    var countdown=60;
    function beginTime() {
        $("#checkCodeMobile").text(countdown + "秒后获取");
        if (countdown == 1) {
            $("#checkCodeMobile").text("获取验证码");
            //进度百分百后，清除掉定时器
            clearInterval(timer);
            return false;
        } else {
            if (countdown == 60) {
                var mobile = $("#mobile").val();
                $.ajax({
                    url: baseUrl + '/user-center-oauth/public/login/captcha/send?username=' + mobile,
                    type: "post",
                    dataType: "json",
                    success: function (data) {
                        if (data != "") {
                            if (data.code == 200) {
                                alert("验证码发送成功");
                            } else {
                                alert(data.msg);
                                $("#checkCodeMobile").text("获取验证码");
                                //进度百分百后，清除掉定时器
                                clearInterval(timer);
                            }
                        }
                    }
                });
            }
            countdown--;
        }
    }
}


// 手机号码/邮箱登录
function doLoginMobile(){
    if(!checkMobile()){
        return false;
    }
    if(!checkPassword()){
        return false;
    }
    // 判断验证码是否输入
    var checkCode = $("#checkCode").val();
    if(checkCode == null || checkCode == ""){
        $("#checkCodeError").text("请输入验证码！");
        $("#checkCodeError").show();
        return false;
    }
    $("#checkCodeError").hide();

    var mobile = $("#mobile").val();
    var password = $("#password").val();
    var passwordEn = encrypt(password, "e0da368a8fa3d9b3f691e82957ec5b75").toUpperCase();
    /*var passwordEn = '055A86DD7F920F98FFBB9938ADD3F4DF';*/
    var data = {username: mobile, password: passwordEn, captcha: checkCode};
    $.ajax({
        url: '/user-center-oauth/login',
        type: "post",
        data: JSON.stringify(data),
        beforeSend: function (req) {
            req.setRequestHeader('Auth', 'fdfed217-8f49-43b4-877d-cc5db8795288')
        },
        headers: {'Content-Type': 'application/json'},
        dataType: "json",
        success: function (data) {
            if (data != "" && data.status == 200) {
                setLocalStorage('ACCESS_TOKEN', data.token);
                queryUserDetail();
            }else {
                alert(data.message);
            }
        },
        error: function (data){
            alert(data.responseJSON.message);
        }
    });
}

function queryUserDetail(){
    $request({
        url: '/user-center-oauth/oauthUser/detail',
        method: 'get'
    }, function (res) {
        setLocalStorage('username', res.username);
        setLocalStorage('nickname', res.nickname);
        window.location.href = "home.html";
    });
}

// 设置localstorage
function setLocalStorage(key, val) {
    if (window.localStorage) {
        localStorage.setItem(key, val);
    }
}

// 添加设置cookie
function setCookie(name, value, days){    //封装一个设置cookie的函数
    var date = new Date();
    date.setTime(date.getTime() + (days*24*60*60*1000));
    //格林威治时间 (GMT)
    var expires="expires=" + date.toGMTString();
    document.cookies = name + "=" + value + ";" + expires;
}

function getCookie(name) {
    //格式化
    var name=name+"=";
    //分割
    var ca=document.cookies.split(";");
    //遍历
    for(var i=0;i<ca.length;i++){
        var c=ca[i].trim();
        //是否包含用户名
        if(c.indexOf(name)==0){
            return c.substring(name.length,c.length);
        }
    }
    return "";
}